import {_decorator, Button, Component, director, Node} from 'cc';
import Long from "long";
import {GameEvent} from "db://assets/script/game-event";
import {FishAliveEvent} from "db://assets/script/common/events/fish-events/fish-alive-event";
import {userInfo} from "db://assets/script/user-info";
import {showAlert} from "db://assets/script/game/alert/alert";
import {UserFishReceiveBagEvent} from "db://assets/script/common/events/fish-events/user-fish-receive-bag-event";

const {ccclass, property} = _decorator;

@ccclass('FishAliveDurationSelector')
export class FishAliveDurationSelector extends Component {

    @property(Button)
    shortAliveBtn!: Button

    @property(Button)
    middleAliveBtn!: Button

    @property(Button)
    longAliveBtn!: Button

    fishId!: Long
    zoneId!: Long

    init(fishId: Long, zoneId: Long) {
        this.fishId = fishId;
        this.zoneId = zoneId;
    }

    onEvent() {
        this.shortAliveBtn.node.on(Button.EventType.CLICK, () => this.doAlive(1))
        this.middleAliveBtn.node.on(Button.EventType.CLICK, () => this.doAlive(6))
        this.longAliveBtn.node.on(Button.EventType.CLICK, () => this.doAlive(12))
    }

    offEvent() {
    }

    start() {
        this.onEvent()
    }

    onDestroy() {
        this.offEvent()
    }

    private doAlive(hour: number): void {
        const ff = userInfo.fishInfo.loadFishById(this.fishId)
        if (ff) {
            if (Object.keys(ff.assetInfo!.props).length > 0) {
                showAlert('道具领取', '弟子行囊还有道具未领取，是否领取？', () => {
                    director.emit(GameEvent.userFishRequestReceiveFishBag, new UserFishReceiveBagEvent(ff.id, ff.zoneId.toString(), null, () => {
                        director.emit(GameEvent.fishActionActive, new FishAliveEvent(this.fishId, this.zoneId, Long.fromNumber(hour * 3600000)))
                        this.node.destroy()
                    }))
                })
            } else {
                director.emit(GameEvent.fishActionActive, new FishAliveEvent(this.fishId, this.zoneId, Long.fromNumber(hour * 3600000)))
                this.node.destroy()
            }
        } else {
            director.emit(GameEvent.fishActionActive, new FishAliveEvent(this.fishId, this.zoneId, Long.fromNumber(hour * 3600000)))
            this.node.destroy()
        }
    }

}